// FileName: Common.ts
// ProjectName: miu-club
// 作者：区区电脑
// createTime: 2019/9/18

//// dialog
import {
    Confirm,
    Alert,
    Toast,
    Notify,
    Loading
    // @ts-ignore
} from 'vue-ydui/dist/lib.rem/dialog';
import {Vue} from "vue-property-decorator";
import GConfirm from "@/common/gview/GConfirm";

class Dialog {
    public confirm!: Function;
    public alert: any = Alert;
    public toast: any = Toast;
    public notify: any = Notify;
    public loading: any = Loading;
    constructor() {
        this.confirm = new GConfirm().confirm;
    }
}

class Common extends Dialog{
    constructor() {
        super();
    }
    /**
     * 使页面滚动到顶部
     */
    public scrollToTop(): void {
        // @ts-ignore
        document.getElementById('g-html').scrollTop = 0;
    }

    /**
     * 停止下拉刷新
     * @param vue 当前vue 对象
     * @param ref ydui 下拉刷新组件 ref
     */
    public stopPullDownRefresh(vue: Vue, ref: string): void {
        try {
            // @ts-ignore
            vue.$refs[ref].$emit('ydui.pullrefresh.finishLoad')
        } catch (e) {
            // @ts-ignore
            // eslint-disable-next-line no-console
            console.log('pullrefresh.finishLoad')
        }
    }
}

class Model {
    /**
     * 根据数组获取对应的模型数组
     * @param array
     */
    public static getModelsWithArray(array: any[]): Model[] {
        let arr: Model[] = [];
        for (let i = 0; i < array.length; i++) {
            let model: Model = new Model();
            for (let key in array[i]) {
                // @ts-ignore
                model[key] = array[i][key];
            }
            arr.push(model);
        }
        return arr;
    }

    /**
     * 获取模型
     * @param data
     */
    public static getModel(data: any): Model {
        let model:Model = new Model();
        for (let key in data) {
            // @ts-ignore
            model[key] = data[key];
        }
        return model;
    }
}


export {
    Dialog,
    Common,
    Model
}
